#Rem
Header: Bouding Box. 
#End

Import mojo
Import agfx

#Rem
Summary: BOUNDING BOX CLASS
#End
Class TBBox

	#Rem
	Summary: top left position
	#End
	
	Field Min:TVector2
	
	#Rem
	Summary: bottom right position
	#End
	
	Field Max:TVector2

	#Rem
	Summary: size of BBOX stored as vector
	#End
	Field size:TVector2
	#Rem
	Summary: half size of size
	#End
	Field size_half:TVector2
	#Rem
	Summary: center of BBOX
	#End
	Field center:TVector2
	#Rem
	Summary: radius
	#End
	Field radius:Float
	
	#Rem
	Summary: Constructor
	#End
	Method New()
		Self.size 		= New TVector2(0,0)
		Self.Min 		= New TVector2(0,0)
		Self.Max 		= New TVector2(0,0)
		Self.size_half	= New TVector2(0,0)
		Self.center  	= New TVector2(0,0)
		Self.radius 	= 0
	End Method
	
	#Rem
	Summary: Set BBOX 
	c : center
	s : size
	r : radius
	#End
	Method Set(c:TVector2,s:TVector2,r:Float)
		Self.center.Set(c)
		Self.size.Set(s)
		Self.size_half = VecMult(s,0.5)
		Self.radius = r		
		Self.Min = VecSub(Self.center,Self.size_half)
		Self.Max = VecAdd(Self.center,Self.size_half)
	End Method
	
	#Rem
	Summary: recalculate top-left and bottom-right position of BBOX from half size
	#End
	Method UpdateByCenter(c:TVector2)
		Self.center.Set(c)
		Self.Min = VecSub(Self.center,Self.size_half)
		Self.Max = VecAdd(Self.center,Self.size_half)
	End Method
	
	#Rem
	Summary: Draw visual debug information
	#End
	Method DebugDraw_Corners:Void()
		Local l:Int=16
		SetColor(255,255,0)	
		Local pA:TVector2 = New TVector2(Self.Min)	
		Local pB:TVector2 = New TVector2(Self.Max.x,Self.Min.y)	
		Local pC:TVector2 = New TVector2(Self.Max)	
		Local pD:TVector2 = New TVector2(Self.Min.x,Self.Max.y)	
		
		DrawLineVector(pA,VecAdd(pA,New TVector2(0,l)))
		DrawLineVector(pA,VecAdd(pA,New TVector2(l,0)))
		
		DrawLineVector(pB,VecAdd(pB,New TVector2(-l,0)))
		DrawLineVector(pB,VecAdd(pB,New TVector2(0,l)))
		
		DrawLineVector(pC,VecAdd(pC,New TVector2(-l,0)))
		DrawLineVector(pC,VecAdd(pC,New TVector2(0,-l)))
		
		DrawLineVector(pD,VecAdd(pD,New TVector2(l,0)))
		DrawLineVector(pD,VecAdd(pD,New TVector2(0,-l)))
				
	End Method
	
End Class

#Rem
Footer: ------. 
#End